package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import com.mysql.jdbc.Statement;

public class MemberDao extends ConnectionBase implements IMemberDao {

	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;
	
	@Override
	public Map<String, Object> getMemberByUid(Map<String, String> map) throws Exception {
		Map<String, Object> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_user where u_id=? and shop_id=?";
			conn = getConn();
			result = qRunner.query(conn, sql, new MapHandler(), map.get("userId"), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public Map<String, Object> getMemberByOpenid(Map<String, String> map) throws Exception {
		Map<String, Object> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_user where u_openid=? and shop_id=?";
			conn = getConn();
			result = qRunner.query(conn, sql, new MapHandler(), map.get("openId"), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public long saveMember(Map<String, String> map) throws Exception {
		long r=0;
		String sqlStr = "insert into tb_user(shop_id, u_openid) values(?,?)";
		try {
			conn = getConn();
			conn.setAutoCommit(false);       //不自动提交  
			ps= conn.prepareStatement(sqlStr,Statement.RETURN_GENERATED_KEYS);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("openId"));
			int re = ps.executeUpdate();
			ResultSet rsKey = ps.getGeneratedKeys();
			while(re!=0&&rsKey.next())
			{
				r = rsKey.getLong(1);
			}
			//创建积分数据
			String integralSql = "insert into tb_integral(shop_id, u_id, i_total, i_remain, g_grade) values(?,?,0,0,0)";
			ps= conn.prepareStatement(integralSql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, r);
			boolean f = ps.executeUpdate() > 0 ? true : false;
			if(f) {
				conn.commit();
				conn.setAutoCommit(true);     //设回自动提交
				return r;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return 0;
	}

	@Override
	public boolean updateMember(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_user set u_nickname=?, u_avatarUrl=?, u_gender=? where shop_id=? and u_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps= conn.prepareStatement(sql);
			ps.setObject(1, map.get("wxNickName"));
			ps.setObject(2, map.get("wxAvatarUrl"));
			ps.setObject(3, map.get("wxGender"));
			ps.setObject(4, map.get("shopId"));
			ps.setObject(5, map.get("userId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean modifyUserTable(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_user set u_label=? where shop_id=? and u_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps= conn.prepareStatement(sql);
			ps.setObject(1, map.get("label"));
			ps.setObject(2, map.get("shopId"));
			ps.setObject(3, map.get("userId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

	
}
